package com.thread.xxstudy;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 创建SingleThreadExecutor 只会有一个线程执行任务也就是   并且是顺序执行   先加入的线程先运行
 *  关于多线程中出现异常的问题  如果不涉及本句  不会阻碍其他语句的运行
 */
public class SingleThreadPool {


   /* public static void main(String[] args) {
        new SingleThreadPool().run1();
    }*/
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    public void run1() {

        SingleThreadPool otherThreadPool = new SingleThreadPool();
        SingleThread1 singleThread1 = otherThreadPool.new SingleThread1();
        SingleThread2 singleThread2 = otherThreadPool.new SingleThread2();

        executorService.submit(singleThread1);
        executorService.submit(singleThread2);
    }

    class SingleThread1 implements Runnable {
        int a = 0;

        @Override
        public void run() {
            while (true) {
                if (a >= 100) {

                        int activeCount = ((ThreadPoolExecutor) executorService).getActiveCount();
                        System.out.println(activeCount);

                    break;
                }
                System.out.println(Thread.currentThread().getName() + "----" + (++a) + "-=-=-=-"   );
            }
        }
    }

    class SingleThread2 implements Runnable {
        int a = 100;

        @Override
        public void run() {
            while (true) {
                if (a == 0) {
                    break;
                }
                System.out.println(Thread.currentThread().getName() + "----=" + (--a));
            }
        }
    }

}
